package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.IndexImg;
import com.yami.shop.bean.model.LuckProduct;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.LuckProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/luckProduct")
public class LuckProductController{

    private List luckProductCach= new ArrayList<String>();

    @Autowired
    private LuckProductService luckProductService;

    /**
     * 分页获取
     */
    @GetMapping("/lists")
    public ResponseEntity<IPage<LuckProduct>> page(LuckProduct luckProduct, PageParam<LuckProduct> page) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        IPage<LuckProduct> indexImgIPage = luckProductService.page(page,
                new LambdaQueryWrapper<LuckProduct>().eq(LuckProduct::getShopId,shopId)
                        .orderByDesc(LuckProduct::getId));
        return ResponseEntity.ok(indexImgIPage);
    }

//    @RequestMapping("/lists")
//    public ResponseEntity getLuckProductList(){
//        return  ResponseEntity(luckProductService.queryList(null));
//    }

    @RequestMapping("/showlists")
    public ResponseEntity getLuckProductByShowList(){
        LuckProduct luckProduct = new LuckProduct();
        luckProduct.setFlag(1);
        List<LuckProduct> luckProducts = luckProductService.queryList(luckProduct);
        if(luckProduct!=null&&luckProducts.size()>0){
            List<String> collect = luckProducts.stream().map((lp) -> {
                return lp.getName();
            }).collect(Collectors.toList());
            return ResponseEntity.ok(collect);
        }
        return ResponseEntity.badRequest().body("");
    }


    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<String> save(@Valid @RequestBody LuckProduct luckProduct) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        luckProduct.setFlag(1);
        luckProduct.setShopId(shopId);
        luckProductService.save(luckProduct);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody @Valid LuckProduct luckProduct) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        luckProduct.setShopId(shopId);
        luckProductService.updateById(luckProduct);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestBody Long[] ids) {
        luckProductService.deleteLuckProductByIds(ids);
        return ResponseEntity.ok().build();
    }

}
